package com.fhj.movie.fragment;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.fhj.movie.activity.HyyGuanYingZhiNanActivity;
import com.fhj.movie.activity.HyyItemClick;
import com.fhj.movie.activity.HyyTeJiaHuoDong;
import com.fhj.movie.entity.BannerList;
import com.fhj.movie.entity.HotList;
import com.fhj.movie.entity.HuoDongList;
import com.fhj.movie.interfaces.CallbackInterface;
import com.fhj.movie.view.MyListView;
import com.fhj.movie_fhj_01.R;
import com.fhj.utils.CacheUtils;
import com.fhj.utils.HttpConnectionUtils;
import com.fhj.utils.JsonUtils;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.lidroid.xutils.BitmapUtils;

public class HyyHuoDongFragment extends Fragment implements OnItemClickListener {

	private ViewFlipper viewFlipper;

	private List<HotList> list;

	private RequestQueue requestQueue;

	private CacheUtils cache;

	private MyListView listView;

	private MyAdapter adapter;
	
	private PullToRefreshScrollView scrollView;

	private HuoDongList huoDongLists;

	private ImageView imageView,xuanzhuan;
	
	private Button button1, button2;

	private String[] str;
	
	private BitmapUtils bitmapUtils;

	private LinearLayout layout;
	
	private Animation loadAnimation;
	
	private int count=1;
	public HyyHuoDongFragment() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		//count=savedInstanceState.optInt("count",1);
		requestQueue = Volley.newRequestQueue(getActivity());
		list = new ArrayList<HotList>();
		bitmapUtils=new BitmapUtils(getActivity());
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.fragment_huo_dong, container,
				false);
		scrollView=(PullToRefreshScrollView)view.findViewById(R.id.pullToRefreshScrollView);
		listView = (MyListView) view.findViewById(R.id.hotlist);
		//View view1 = inflater.inflate(R.layout.listview_header, null, false);
		viewFlipper = (ViewFlipper) view.findViewById(R.id.add_image);
		button1 = (Button) view.findViewById(R.id.tejiahuodong);
		button2 = (Button) view.findViewById(R.id.guanyingzhinan);
		xuanzhuan=(ImageView)view.findViewById(R.id.huodongXuanzhuan);
		layout=(LinearLayout)view.findViewById(R.id.huodongqianye);
		loadAnimation = AnimationUtils.loadAnimation(getActivity(), R.anim.taste);
		LinearInterpolator lir = new LinearInterpolator();
		loadAnimation.setInterpolator(lir);
		if(count==1){
			layout.setVisibility(View.VISIBLE);
			xuanzhuan.setVisibility(View.VISIBLE);
			xuanzhuan.startAnimation(loadAnimation);
		}
		// imageView1=(NetworkImageView)view1.findViewById(R.id.viewFlipper_image1);
		// imageView1=(NetworkImageView)view1.findViewById(R.id.viewFlipper_image2);
	
		
//		listView.addHeaderView(view1);
		adapter = new MyAdapter();
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(this);
		// Utility.setListViewHeightBasedOnChildren(listView);
		HttpConnectionUtils.getActivityData(getActivity(), 440300,
				new CallbackInterface() {
				
					@Override
					public void JsonCallback(JSONObject object) {
						// TODO Auto-generated method stub
						count++;
						xuanzhuan.clearAnimation();
						xuanzhuan.setVisibility(View.INVISIBLE);
						layout.setVisibility(View.INVISIBLE);
						huoDongLists = JsonUtils.parseData(object);
						
						final List<BannerList> bannerLists = huoDongLists

						.getBannerLists();
						str=new String[bannerLists.size()];
						for(int i=0;i<bannerLists.size();i++){
							final int count=i;
							String s = bannerLists.get(i).getPicUrl();
							imageView=new ImageView(getActivity());
							imageView.setImageResource(R.drawable.promotion_cell_default);
							LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
									LinearLayout.LayoutParams.WRAP_CONTENT);  // , 1是可选写的
							lp.setMargins(5, 0, 5, 0); 
							imageView.setLayoutParams(lp); 
							imageView.setScaleType(ScaleType.FIT_XY);
							imageView.setOnClickListener(new OnClickListener() {
								
								@Override
								public void onClick(View v) {
									// TODO Auto-generated method stub
									Intent intent = new Intent(getActivity(), HyyItemClick.class);
									intent.putExtra("title", bannerLists.get(count).getTitle());
									intent.putExtra("clickUrl", bannerLists.get(count).getClickUrl());
									startActivity(intent);
								}
							});
							bitmapUtils.display(imageView, s);
							viewFlipper.addView(imageView);
						}
						viewFlipper.setAutoStart(true);
						list = huoDongLists.getHotLists();
						Log.i("", "----------------------->>" + list.toString());
						adapter.notifyDataSetChanged();
					
					}
				});
		// scrollView.setOnRefreshListener(listener)
		button1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(getActivity(),HyyTeJiaHuoDong.class);
				startActivity(intent);
				
			}
		});
		button2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(getActivity(),HyyGuanYingZhiNanActivity.class);
				startActivity(intent);
			}
		});scrollView.setScrollingWhileRefreshingEnabled(false);
		 scrollView.setMode(Mode.PULL_FROM_START);  
		scrollView.setOnRefreshListener(new OnRefreshListener<ScrollView>() {

			@Override
			public void onRefresh(PullToRefreshBase<ScrollView> refreshView) {
				// TODO Auto-generated method stub
				HttpConnectionUtils.getActivityData(getActivity(), 440300,
						new CallbackInterface() {

							@Override
							public void JsonCallback(JSONObject object) {
								// TODO Auto-generated method stub

								huoDongLists = JsonUtils.parseData(object);
								
								final List<BannerList> bannerLists = huoDongLists

								.getBannerLists();
								str=new String[bannerLists.size()];
								for(int i=0;i<bannerLists.size();i++){
									final int count=i;
									String s = bannerLists.get(i).getPicUrl();
									imageView=new ImageView(getActivity());
									imageView.setImageResource(R.drawable.promotion_cell_default);
									LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
											LinearLayout.LayoutParams.WRAP_CONTENT);  // , 1是可选写的
									lp.setMargins(5, 0, 5, 0); 
									imageView.setLayoutParams(lp); 
									imageView.setScaleType(ScaleType.FIT_XY);
									imageView.setOnClickListener(new OnClickListener() {
										
										@Override
										public void onClick(View v) {
											// TODO Auto-generated method stub
											Intent intent = new Intent(getActivity(), HyyItemClick.class);
											intent.putExtra("title", bannerLists.get(count).getTitle());
											intent.putExtra("clickUrl", bannerLists.get(count).getClickUrl());
											startActivity(intent);
										}
									});
									bitmapUtils.display(imageView, s);
									viewFlipper.addView(imageView);
								}
								viewFlipper.setAutoStart(true);
								list = huoDongLists.getHotLists();
								Log.i("", "----------------------->>" + list.toString());
								adapter.notifyDataSetChanged();
								scrollView.onRefreshComplete();
							}
						});
				
			}
		});
		return view;
	}

	class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-gehonerated method stub
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = LayoutInflater.from(getActivity()).inflate(
						R.layout.fragment_lhuo_dong_list_tem, parent, false);
				holder.item_image = (ImageView) convertView
						.findViewById(R.id.item_image);
				holder.item_title = (TextView) convertView
						.findViewById(R.id.item_title);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			String url = list.get(position).getPicUrl();
			holder.item_image.setImageResource(R.drawable.place_holder_h);
			// TODO 下载图片
			if (url != null) {
				bitmapUtils.display(holder.item_image, url);
			}
			holder.item_title.setText(list.get(position).getTitle());

			return convertView;
		}

	}

	class ViewHolder {
		public ImageView item_image;
		public TextView item_title;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		// String path=list.get(position).getClickUrl();
		Intent intent = new Intent(getActivity(), HyyItemClick.class);
		intent.putExtra("title", list.get(position).getTitle());
		intent.putExtra("clickUrl", list.get(position).getClickUrl());
		startActivity(intent);
	}
	
}
